package com.gitee.wsl.ext.map


/**
 * 将dataMap 分成 若干组，每组size条
 * @param dataMap 需要分组的Map
 * @param size 每组分多少条
 * @return
 * @param <K>
 * @param <V>
</V></K> */
fun <K, V> Map<K, V>.partition(size: Int): List<Map<K, V>> {
    val dataMap = this
    if (dataMap.isEmpty()) {
        throw NullPointerException("dataMap is empty")
    }
    if (size <= 0) {
        throw IllegalArgumentException("size cannot <= 0")
    }

    val dataList: MutableList<Map<K, V>> = ArrayList<Map<K, V>>()

    var mapGroup: MutableMap<K, V> = HashMap<K, V>()

    for ((key, value) in dataMap) {
        if (mapGroup.size < size) {
            mapGroup[key] = value
            continue
        }

        dataList.add(mapGroup)

        mapGroup = HashMap<K, V>()
        mapGroup[key] = value
    }

    if (mapGroup.isNotEmpty()) {
        dataList.add(mapGroup)
    }

    return dataList
}